package com.ifast.api.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.beust.jcommander.internal.Maps;
import com.ifast.api.service.UserService;
import com.luculent.util.extension.api.Result;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * <pre>
 *  基于jwt实现的API测试类
 * </pre>
 * 
 * <small> 2018年4月27日 | Aron</small>
 */
@RestController
@RequestMapping("/api/login/")
public class ApiLoginController {

    @Autowired
    private UserService userService;

    @PostMapping("login")
    @ApiOperation(value = "登录", httpMethod = "POST",notes="")
    public Result<?> token(@ApiParam(name = "userName", required = true, value = "用户名") @RequestParam String userName,
                               @ApiParam(name = "passWord", required = true, value = "密码") @RequestParam String passWord) {
        Map<String,String> map = Maps.newHashMap();
        map.put("token",userService.getToken(userName, passWord).getToken());
    	return Result.ok(map);
    }

    @PostMapping("logout")
    @ApiOperation(value = "退出登录", httpMethod = "POST",notes="")
    public Result<?> logout(@ApiParam(name = "Authorization", required = true, value = "token") @RequestHeader("Authorization") String token) {
        userService.logoutToken(token,"");
        return Result.ok();
    }

}
